package com.czl.module_park.viewmodel

import com.blankj.utilcode.util.ApiUtils
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.binding.command.BindingConsumer
import com.czl.base.config.AppConstants
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.CarAceessItemBean
import com.czl.base.data.bean.CarItem
import com.czl.base.data.bean.UserInfoBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriber200Helper
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper
import com.trello.rxlifecycle3.android.RxLifecycleAndroid
import io.reactivex.Observable
import io.reactivex.ObservableSource
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import io.reactivex.functions.Function
import io.reactivex.schedulers.Schedulers
import java.lang.Exception

class CarAccessViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {
    var actionAble=true
    val loadCompleteEvent:SingleLiveEvent<List<CarAceessItemBean>> = SingleLiveEvent()
//    val inputChangeEvent:SingleLiveEvent<String> = SingleLiveEvent()
    val onLoadMoreListener: BindingCommand<Void> = BindingCommand(BindingAction {

    })
    val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
        getCarAccessList()
    })
    fun getCarAccessList(){
        model.getUserDetailInfo().subscribeOn(Schedulers.io())
            .unsubscribeOn(Schedulers.io())
            .observeOn(Schedulers.io())
            .doOnSubscribe(this)
            .concatMap { userbean ->
                val observable = if (userbean.code == 200) {
                    actionAble = AppConstants.SysytemPermission.car_aceess in userbean.permissions
                    model.getCarAccessList(model.getAreaId())
                } else {
                    Observable.error(Throwable(userbean.msg))
                }
                observable
            }.subscribeOn(Schedulers.io())
            .unsubscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .doOnSubscribe(this).subscribe(object : ApiSubscriber200Helper<BaseBean<List<CarAceessItemBean>>>() {
                override fun onResult(t: BaseBean<List<CarAceessItemBean>>) {
                    for (item in t.data!!){
                        item.hasPermission=actionAble
                    }
                    loadCompleteEvent.postValue(t.data)
                }
                override fun onFailed(msg: String?) {
                    showErrorToast(msg)
                    loadCompleteEvent.postValue(null)
                }
            })
    }
    fun carAccessAction(item:CarAceessItemBean){
        model.carAccessAction(mapOf("areaId" to item.areaId,
            "deviceGuid" to item.deviceGuid,
        "deviceId" to item.deviceId,
        "openModel" to "1",
        "parkId" to item.parkId))
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .doOnSubscribe { showLoading() }
            .subscribe(object : ApiSubscriber200Helper<BaseBean<Any?>>() {
            override fun onResult(t: BaseBean<Any?>) {
                dismissLoading()
                showSuccessToast("抬杆成功")
            }

            override fun onFailed(msg: String?) {
                dismissLoading()
                showErrorToast(msg)
            }

        })
    }
}
